package com.sun.module_common.widget

import android.annotation.SuppressLint
import androidx.activity.ComponentActivity
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import com.gyf.immersionbar.ImmersionBar

/**
 *@author:sunc
 *@time :2025/8/12 11:30
 *@desc: 设置状态栏颜色
 */

@SuppressLint("ContextCastToActivity")
@Composable
fun OptimizedStatusBar(
    color: Color,
    darkIcons: Boolean = !isSystemInDarkTheme()
) {
    val currentColor = remember { mutableStateOf(Color.Transparent) }
    val activity = LocalContext.current as ComponentActivity
    val animatedColor by animateColorAsState(targetValue = color, animationSpec = tween(300))

    SideEffect {
        if (color != currentColor.value) {
            ImmersionBar.with(activity).statusBarColorInt(animatedColor.toArgb())
                .statusBarDarkFont(darkIcons).init()
            currentColor.value = color
        }
    }
}